package com.fxd.dao;

import com.fxd.doman.Account;
import com.fxd.doman.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * @auther fuxudong
 * @date 2021/1/10 - 12:27
 * 在mybatis中针对，crud一共有四个注解
 * @Select @Update @Insert @Delete
 */
//开启二级缓存，默认false
@CacheNamespace(blocking = true)
public interface IUserDao {

    @Select("select*from user")
    @Results(id="accountMap",value = {
            @Result(id=true,column = "id" ,property = "id"),
            @Result(column = "username" ,property = "username"),
            @Result(column = "address" ,property = "address"),
            @Result(column = "sex" ,property = "sex"),
            @Result(column = "birthday" ,property = "birthday"),
            //select方法全限定名称，column表示参数，fetchtype，eager:立即加载，lazy:延迟加载
            @Result(property = "accounts",column = "id",many=@Many( select="com.fxd.dao.IAccountDao.findById",fetchType= FetchType.LAZY))

    })
    List<User> findAll();

    @Insert("insert into user(username,address,sex,birthday) values (#{username},#{address},#{sex},#{birthday})")
    void savaUser(User user);

    @Update("update user set username=#{username},sex=#{sex},birthday=#{birthday},address=#{address} where id=#{id}")
    void updateUser(User user);

    @Delete("delete from user where id=#{id}")
    void deleteUser(Integer userId);

    @Select("select * from user where id=#{id}")
    User findById(Integer userId);

    //@Select("select * from user where username like #{username}")
    //value是固定的 会引发SQL注入问题
    @Select("select * from user where username like '%${value}%'")
    List<User> findByName(String username);

    @Select("select count(*) from user")
    int findTotalUser();
}
